package cn.imokay;

import org.junit.Test;

import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;

/*
*匿名内部类的优化
*
* */

public class LambdaDemo {

    @Test
    public void noLambdaDemoTest(){
        noLambdaDemo();
    }

    public static void noLambdaDemo() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("------------新线程被start-----------");
            }
        }).start();
    }

    @Test
    public void lambdaDemoTest(){
        lambdaDemo();
    }

    public static void lambdaDemo() {
        //简化
        new Thread(() -> {
            System.out.println("------------新线程被start-----------");
        }).start();
    }

    @Test
    public void calculateNoLambdaTest() {
        int calculate = calculate(new IntBinaryOperator() {
            @Override
            public int applyAsInt(int left, int right) {

                return left + right;
            }
        });

        System.out.println(calculate);
    }

    @Test
    public void calculateLambdaTest() {
        int calculate = calculate((left, right) -> left + right);
        System.out.println(calculate);
    }
    public static int calculate(IntBinaryOperator operator) {
        int a =10;
        int b = 20;
        return operator.applyAsInt(a,b);
    }

    @Test
    public void printNumNoLambdaTest() {
        printNum(new IntPredicate() {
            @Override
            public boolean test(int value) {
                return value % 2 == 0;
            }
        });
    }

    @Test
    public void printNumLambdaTest() {
        printNum(value -> value % 2 == 0);
    }


    public static void printNum(IntPredicate predicate) {
        int[] nums = {1,2,3,4,5,6,7,8,9};
        for (int num : nums) {
            if (predicate.test(num)) {
                System.out.println(num);
            }
        }
    }

    @Test
    public void typeConvertNoLambdaTest(){
        Integer i = typeConvert(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.valueOf(s);
            }
        });

        System.out.println(i);
    }

    @Test
    public void typeConvertLambdaTest(){
        Integer i = typeConvert(s -> Integer.valueOf(s));

        System.out.println(i);

        String s1 = typeConvert(s -> s + "123");
        System.out.println(s1);
    }

    public static <R> R typeConvert(Function<String,R> function){
        String s = "12345";
        return function.apply(s);
    }


    @Test
    public void foreachArryNoLambdaTest(){
        foreachArry(new IntConsumer() {
            @Override
            public void accept(int value) {
                System.out.println(value);
            }
        });
    }

    @Test
    public void foreachArryLambdaTest(){
        foreachArry((value) ->{
            System.out.println(value);
        });
    }

    public static void foreachArry(IntConsumer intConsumer) {
        int[] nums = {1,2,3,4,5,6,7,8,9};
        for (int num : nums) {
            intConsumer.accept(num);
        }
    }
}